<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en"><head>
    <meta charset="utf-8">
    <title>教师注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <style type="text/css">
      body{
        padding-top: 40px;
        padding-bottom: 40px;
        
      }

      .form-signin {
        max-width: 300px;
        padding: 19px 29px 29px;
        margin: 50 auto 20px;
        background-color: #fff;
        border: 1px solid #e5e5e5;
        -webkit-border-radius: 5px;
           -moz-border-radius: 5px;
                border-radius: 5px;
        -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
           -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                box-shadow: 0 1px 2px rgba(0,0,0,.05);
      }
      .form-signin .form-signin-heading,
      .form-signin .checkbox {
        margin-bottom: 10px;
      }
      .form-signin input[type="text"],
      .form-signin input[type="password"] {
        font-size: 16px;
        height: auto;
        margin-bottom: 15px;
        padding: 7px 9px;
      }

    </style>
    <link rel="stylesheet" type="text/css" href="<%=path %>/res/common/bootstrap-3.3.7-dist/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="<%=path %>/res/common/bootstrap-3.3.7-dist/css/bootstrap-theme.css"/>
 	
 	<script src="<%=path %>/res/common/js/jquery-1.11.1.js" type="text/javascript" charset="utf-8"></script>
	<script src="<%=path %>/res/common/js/jquery.validate.js" type="text/javascript" charset="utf-8"></script>
	<script src="<%=path %>/res/common/js/additional-methods.js" type="text/javascript" charset="utf-8"></script>
		
  	<script type="text/javascript">			
			$(function() {
				// 生成一个简单的验证码
				function randomNumber(min, max) {
					return Math.floor(Math.random() * (max - min + 1) + min);
				};
				function generateCaptcha() {
					$('#captchaOperation').html([randomNumber(1, 50), '+', randomNumber(1, 50), '='].join(' '));
				};
				generateCaptcha();
				function getValue() {
					var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
					return value = sum;
				}
				document.getElementById("yz1").value=getValue(); 
				//让当前表单调用validate方法，实现表单验证功能
				$("#myform").validate({
					//debug:true,	//调试模式，即使验证成功也不会跳转到目标页面
					onkeyup: null,
					rules: { //配置验证规则，key就是被验证的dom对象，value就是调用验证的方法(也是json格式)
						id: {
							required: true,
							minlength:2,
							maxlength:20
						},
						pwd: {
							required: true,
							minlength:6,
							maxlength:20
						},
						pwd2: {
							required: true,
							equalTo: '#pwd',
							minlength:6,
							maxlength:20
						},
						yz2:{
						required: true,
						number:true,
						equalTo: '#yz1' //表示和id="captcha"的值相同
						},
						email: {
							required: true,
							email:true,
							maxlength:20
						},
					},
					messages: {
						id: {
							required: "请输入用户名",
							minlength:"用户名长度至少2",
							maxlength:"用户名长度不能大于20"
						},
						pwd: {
							required: "请输入密码",
							minlength:"密码长度至少6",
							maxlength:"密码长度不能大于20"
						},
						pwd2: {
							required: "请确认密码",
							equalTo:"两次密码不一样",
							minlength:"密码长度至少6",
							maxlength:"密码长度不能大于20"
						},
						yz2:{
							required: "请输入验证码",
							number:"只能输入数字",
							equalTo:"验证码错误"
						},
						email: {
							required: "请输入邮箱",
							email:"请输入邮箱格式",
							maxlength:"邮箱长度不能大于20"
						}
					}
				});
				
				
				
			});
			
		function fun1(){
				var checkmsg= document.getElementById("checkmsg");
				var name= document.getElementById("name");
				if (name.value=="") {
					checkmsg.innerText="不能为空";
					return;
				}else{
					checkmsg.innerText="";
					var userName=name.value;
					var xmlHttp;
					//表示当前浏览器不是ie,如ns,firefox  
			        if(window.XMLHttpRequest) {  
			            xmlHttp = new XMLHttpRequest();  
			        } else if (window.ActiveXObject) {  
			            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
			        }
			        //将获取用户名发送到另一个jsp中去验证  
	            	var url = "userValidate/" + userName + "/" + new Date().getTime()+".do";
					   //设置请求方式为GET，设置请求的URL，设置为异步提交，true为异步，false为同步  
		            xmlHttp.open("GET", url, true);   
		            //将方法地址复制给onreadystatechange属性  
		            //类似于电话号码 
		              
		            //将设置信息发送到Ajax引擎  
		            xmlHttp.send(null);  
		            xmlHttp.onreadystatechange=callback(xmlHttp);  
				}
				
			
			}
			
			//发送请求之后，返回的状体  
		    function callback(xmlHttp) { 
		   
		        //alert(xmlHttp.readyState);  
		        //Ajax引擎状态为成功  
		  
		       alert("检查用户名中......");
		        if (xmlHttp.readyState == 4) {  
		            //HTTP协议状态为成功  
		            //alert(xmlHttp.status);
		            if (xmlHttp.status == 200) {  
		            	//alert("sss"+xmlHttp.responseText);
		                if (xmlHttp.responseText != "") {  
		                    //设置请返回的消息信息  
		                    document.getElementById("checkmsg").innerHTML = xmlHttp.responseText;  
		                }else {  
		                    document.getElementById("checkmsg").innerHTML = "";  
		                }  
		            }else {  
		                alert("请求失败，错误码=" + xmlHttp.status);  
		            }  
		        }  
		    }	
		
		</script>
		
  </head>

  <body style="background: url(<%=path %>/res/common/imgs/loginbeijing1.jpg);">
    <div class="container">
      <form id="myform" action="teacherRegisterPost.do" method="post" class="form-signin">
        <h2 class="form-signin-heading">教师注册</h2>
        <div id="checkmsg" style="color: red;">
        ${usermsg}</div>
        <input class="input-block-level" id="name" name="name" type="text" placeholder="用户名"   
        onBlur="fun1()" />

       	<div style="color: red;">
        ${pwdmsg}</div>
        <input class="input-block-level" id="pwd" name="pwd" type="password" placeholder="密码">
        <div style="color: red;">
        ${pwdmsg2}</div>
        <input class="input-block-level" id="pwd2" name="pwd2" type="password" placeholder="确认密码">
        <div style="color: red;">
        ${emailmsg}</div>
        <input class="input-block-level" id="email" name="email" type="text" placeholder="邮箱">
        <h5>请将如下计算结果填入文本框内：</h5>
		<div class="form-group form-horizontal">
		<label class="col-lg-5 control-label " id="captchaOperation"></label>
		<div class="col-lg-7">
		<input type="text" id="yz1" name="yz1" style="display:none"/>
		<input type="text" class="form-control " id="yz2"  name="yz2" />
		</div>
		</div><br /><br /> <br /> 
        <button class="btn btn-large btn-primary" type="submit">确定</button>
		<a href="teacherLogin.do">返回登陆页</a>
      </form>
    </div> <!-- /container -->

</body></html>